<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020/06/10 15:03
 * Desc: 订单退订模型
 */
class Model_Api_Standard_Refund
{

    /**
     * @function 检查订单是否可以退款并返回退款相关信息
     * @param $orderid
     * @param $mid
     * @return array
     */
    public static function check_refund_info($orderid, $mid)
    {
        // 获取订单信息
        $info = DB::select()->from("member_order")->where("memberid", "=", $mid)->and_where("id", "=", $orderid)->execute()->current();
        if (empty($info))
        {
            return array('status' => false, 'msg' => '查找订单信息失败');
        }
        else
        {
            if ($info['status'] == 2 && $info['refund_restriction'] != 1)
            {
                $orderInfo = Model_Member_Order::order_info($info['ordersn']);
                $online_transaction_no = json_decode($info['online_transaction_no'], true);
                $refund_auto = 0;
                $pay_online_source = '';
                if (!empty($online_transaction_no))
                {
                    $refund_auto = 1;
                    $pay_online_source = $online_transaction_no['source'];
                }
                $result = array(
                    'status' => true,
                    'msg' => '获取成功',
                    'refundData' => array(
                        'typeid' => $info['typeid'],
                        'ordersn' => $info['ordersn'],
                        'pay_price' => $orderInfo['payprice'],
                        'refund_auto' => $refund_auto,
                        'pay_online_source' => $pay_online_source,
                    )
                );
            }
            else
            {
                $msg = $info['refund_restriction'] != 1 ? '' : '订单不可退订';
                $msg = $info['status'] != 2 ? '订单状态异常，请刷新重新' : $msg;
                $result = array('status' => false, 'msg' => $msg);
            }
            return $result;
        }
    }


    /**
     * @function 退款申请
     * @param $data
     * @param $memberid
     * @param $model
     * @return array
     */
    public static function apply_order_refund($data,$memberid,$model)
    {
        $refund_reason = trim(Common::remove_xss($data['refund_reason']));
        $platform =  trim(Common::remove_xss($data['platform']));
        $ordersn =  trim(Common::remove_xss($data['ordersn']));
        $alipay_account ='';
        $cardholder ='';
        $cardnum ='';
        $bank ='';

        if(empty($ordersn))
        {
            return  array('status'=>0,'message'=>'订单号错误');
        }
        $info = Model_Member_Order::order_info($ordersn,$memberid);

        if($info['refund_restriction']==1)
        {
            return  array('status'=>0,'message'=>'不可退款');
        }


        if(empty($info)||$info['status']!=2)
        {
            return  array('status'=>0,'message'=>'订单号错误');
        }
        if(strlen($refund_reason)<5)
        {
            return  array('status'=>0,'message'=>'退款原因不能少于5个字');
        }
        $online_transaction_no = json_decode($info['online_transaction_no'],true);
        //退款到指定账户
        if(empty($online_transaction_no))
        {
            if($platform == 'alipay')
            {
                $alipay_account =  trim(Common::remove_xss($data['alipay_account']));
                $platform = 'other_alipay';
                if(!$alipay_account)
                {
                    return  array('status'=>0,'message'=>'请输入正确的支付宝账号');
                }
            }
            elseif ($platform == 'bank')
            {
                $cardholder =  trim(Common::remove_xss($data['cardholder']));
                $cardnum =  trim(Common::remove_xss($data['cardnum']));
                $bank =  trim(Common::remove_xss($data['bank']));
                if(empty($cardholder)||empty($cardnum)||empty($bank))
                {
                    return  array('status'=>0,'message'=>'请输入正确的银行卡信息');
                }
            }
        }
        //原路退还
        else
        {
            $platform = $online_transaction_no['source'];
        }
        $log = array(
            'ordersn'=>$ordersn,
            'platform'=>$platform,
            'refund_fee'=>$info['payprice'],
            'status'=>0,
            'addtime'=>time(),
            'memberid'=>$memberid,
            'alipay_account'=>$alipay_account,
            'cardholder'=>$cardholder,
            'cardnum'=>$cardnum,
            'bank'=>$bank,
        );
        DB::delete('member_order_refund')->where('ordersn','=',$ordersn)->execute();
        if(self::add_refund_log($log))
        {
            $update = array(
                'status'=>6,
                'refund_reason'=>$refund_reason,
            );
            DB::update('member_order')->set($update)->where('ordersn','=',$ordersn)->execute();
            $order = Model_Member_Order::order_info($ordersn);
            Model_Member_Order::back_order_status_changed($info['status'],$order,$model);
            return  array('status'=>1,'message'=>'退款请求提交成功，等待商家确认!');
        }
        else
        {
            return  array('status'=>0,'message'=>'未知错误');
        }

    }

    /**
     * @function 添加退款日志
     * @param $log
     * @return object
     */
    public static function  add_refund_log($log)
    {
        return DB::insert('member_order_refund',array_keys($log))
            ->values(array_values($log))
            ->execute();
    }
}